package com.ruoyi.epidemic.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.epidemic.mapper.TbUserMapper;
import com.ruoyi.epidemic.domain.TbUser;
import com.ruoyi.epidemic.service.ITbUserService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-21
 */
@Service
public class TbUserServiceImpl implements ITbUserService 
{
    @Resource
    private TbUserMapper tbUserMapper;


    @Value("${user.file.path}")
    private String path;
    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public TbUser selectTbUserById(Long id)
    {
        return tbUserMapper.selectTbUserById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param tbUser 用户
     * @return 用户
     */
    @Override
    public List<TbUser> selectTbUserList(TbUser tbUser)
    {
        return tbUserMapper.selectTbUserList(tbUser);
    }

    /**
     * 新增用户
     * 
     * @param tbUser 用户
     * @return 结果
     */
    @Override
    public int insertTbUser(TbUser tbUser)
    {
        return tbUserMapper.insertTbUser(tbUser);
    }

    /**
     * 修改用户
     * 
     * @param tbUser 用户
     * @return 结果
     */
    @Override
    public int updateTbUser(TbUser tbUser)
    {
        return tbUserMapper.updateTbUser(tbUser);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteTbUserByIds(Long[] ids)
    {
        return tbUserMapper.deleteTbUserByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteTbUserById(Long id)
    {
        return tbUserMapper.deleteTbUserById(id);
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String originName = file.getOriginalFilename();
        String date = format.format(new Date());
        String dirPath = "/" + "app/user" + "/" + date + "/";
        String savePath = path + dirPath;
        // 保存文件的文件夹
        File folder = new File(savePath);
        if (folder.exists()){
            folder.mkdirs();
        }
        String saveName = originName;
        file.transferTo(new File(folder, saveName));
        return dirPath + saveName;
    }

    @Override
    public AjaxResult login(TbUser user) {
        String phone = user.getPhone();
        String password = user.getPassword();
        TbUser phone1 = tbUserMapper.selectOne(new QueryWrapper<TbUser>().eq("phone", phone));
        if (phone1==null){
            return AjaxResult.error(999,"该手机号尚未注册");
        }
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().eq("phone", phone).eq("password", password));
        if (tbUser!=null){
            return AjaxResult.success(tbUser);
        }else {
            return AjaxResult.error(444,"手机号或密码错误");
        }

    }

    @Override
    public AjaxResult register(TbUser user) {
        String username = "";
        String phone = "";
        if (user!=null){
            username = user.getUsername();
            phone = user.getPhone();
            QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
            wrapper.eq("username",username);
            wrapper.eq("phone",phone);
            Long aLong = tbUserMapper.selectCount(wrapper);
            if (aLong==0L){
                int insert = tbUserMapper.insertTbUser(user);
                if (insert!=0) {
                    TbUser tbUser = tbUserMapper.selectOne(wrapper);
                    return AjaxResult.success("注册成功",tbUser);
                }
                return AjaxResult.error("数据保存失败");
            }else {
                return AjaxResult.error("该手机号或用户名已经存在");
            }
        }else {
            return AjaxResult.error();
        }
    }
}
